/**
 * Created by MJiao on 16/2/16.
 */

/**
 * 群防协警
 */
angular.module('app.controllers-auxiliaryPolice', [])


  .controller('AuxiliaryPoliceRecordPersonCtrl',
    ['$scope', 'AuxiliaryPoliceService', '$cordovaToast', 'CommonService',
      function ($scope, AuxiliaryPoliceService, $cordovaToast, CommonService) {

        $scope.$on('$ionicView.beforeEnter', function () {
          //表单验证提示
          $scope.nameErrorTips = {
            required: '请填写姓名'
          };
          $scope.idCardNoErrorTips = {
            required: '请填写身份证',
            maxlength: '二代身份证为18位',
            minlength: '二代身份证为18位'
          };
          $scope.addressErrorTips = {
            required: '请填写地址'
          };
          $scope.collectionCasuseErrorTips = {
            required: '请填写录入原因'
          };
          $scope.sexErrorTips = {
            required: '请选择性别'
          };

          $scope.data = {
            gender: "1"
          };
          $scope.items = [];
        });
        $scope.$on('$ionicView.afterEnter', function () {
          //TODO采用百度地图进行地位置的获取
          CommonService.getLocation().then(function (gpsLocation) {
            $scope.data.collectionAddressGps = gpsLocation;
            $scope.data.collectionAddress = gpsLocation;
          }, function (error) {

          })

          /**
           * form表单提交
           */
          $scope.submit = function () {

            for (var idx = 0; idx < $scope.items.length; idx++) {
              var imageItem = $scope.items[idx];
              var imageIdx = 'image' + (idx + 1);
              $scope.data[imageIdx] = {};
              $scope.data[imageIdx].data = imageItem;
            }
            AuxiliaryPoliceService.recordPersonData($scope.data).then(function (data) {
              $scope.data = {};
              $scope.items = [];
              $cordovaToast.showShortCenter('提交成功');
            }, function (err) {
              $cordovaToast.showShortCenter(err.message);
            })
          }

        });


      }])

  .controller('auxiliaryPoliceRecordVehicleCtrl', ['$scope', 'AuxiliaryPoliceService', '$cordovaToast', 'CommonService',
    function ($scope, AuxiliaryPoliceService, $cordovaToast, CommonService) {

      $scope.$on('$ionicView.beforeEnter', function () {
        //表单验证提示
        $scope.vehicleNoErrorTips = {
          required: '请填写车牌号'
        };
        $scope.involedNameErrorTips = {
          required: '请填写涉事人员姓名'
        };
        $scope.involvedIdCardNoErrorTips = {
          required: '请填写涉事人员身份证号码',
          maxlength: '二代身份证为18位',
          minlength: '二代身份证为18位'
        };
        $scope.collectionCasuseErrorTips = {
          required: '请填写采集原因'
        };
        $scope.involedlivingaddressNoErrorTips = {
          required: '请选涉事人员现住地址'
        };
        $scope.vehicleCategoryErrorTips = {
          required: '请选择车辆类型'
        };

        $scope.vehicleColorErrorTips = {
          required: '请车选择辆颜色'
        };

        $scope.data = {
          involedGener: "1"
        };
        $scope.items = [];
      });
      $scope.$on('$ionicView.afterEnter', function () {
        //TODO采用百度地图进行地位置的获取
        CommonService.getLocation().then(function (gpsLocation) {
          $scope.data.collectionAddressGps = gpsLocation;
          $scope.data.collectionAddress = gpsLocation;
        }, function (error) {

        })

        /**
         * form表单提交
         */
        $scope.submit = function () {

          for (var idx = 0; idx < $scope.items.length; idx++) {
            var imageItem = $scope.items[idx];
            var imageIdx = 'image' + (idx + 1);
            $scope.data[imageIdx] = {};
            $scope.data[imageIdx].data = imageItem;
          }
          AuxiliaryPoliceService.recordVehicleData($scope.data).then(function (data) {
            $scope.data = {};
            $scope.items = [];
            $cordovaToast.showShortCenter('提交成功');
          }, function (err) {
            $cordovaToast.showShortCenter(err.message);
          })
        }

      });

    }])

  .controller('auxiliaryPoliceRecordGoodsCtrl', ['$scope', 'AuxiliaryPoliceService', '$cordovaToast', 'CommonService',
    function ($scope, AuxiliaryPoliceService, $cordovaToast, CommonService) {
      $scope.$on('$ionicView.beforeEnter', function () {
        //表单验证提示
        $scope.goodsNameErrorTips = {
          required: '请填写物品名称'
        };

        $scope.involedNameErrorTips = {
          required: '请填写涉事人员姓名'
        };
        $scope.involvedIdCardNoErrorTips = {
          required: '请填写涉事人员身份证',
          maxlength: '二代身份证为18位',
          minlength: '二代身份证为18位'
        };
        $scope.collectionCasuseErrorTips = {
          required: '请填写采集原因'
        };
        $scope.involedlivingaddressNoErrorTips = {
          required: '请选涉事人员现住地址'
        };


        $scope.data = {
          involedGener: "1"
        };
        $scope.items = [];
      });
      $scope.$on('$ionicView.afterEnter', function () {
        //TODO采用百度地图进行地位置的获取
        CommonService.getLocation().then(function (gpsLocation) {
          $scope.data.collectionAddressGps = gpsLocation;
          $scope.data.collectionAddress = gpsLocation;
        }, function (error) {

        })

        /**
         * form表单提交
         */
        $scope.submit = function () {

          for (var idx = 0; idx < $scope.items.length; idx++) {
            var imageItem = $scope.items[idx];
            var imageIdx = 'image' + (idx + 1);
            $scope.data[imageIdx] = {};
            $scope.data[imageIdx].data = imageItem;
          }
          AuxiliaryPoliceService.recordGoodsData($scope.data).then(function (data) {
            $scope.data = {};
            $scope.items = [];
            $cordovaToast.showShortCenter('提交成功');
          }, function (err) {
            $cordovaToast.showShortCenter(err.message);
          })
        }

      });


    }])

  .controller('auxiliaryPoliceRecordEventCtrl', ['$scope', 'AuxiliaryPoliceService', '$cordovaToast', 'CommonService',
    function ($scope, AuxiliaryPoliceService, $cordovaToast, CommonService) {
      $scope.$on('$ionicView.beforeEnter', function () {
        //表单验证提示
        $scope.involedNameErrorTips = {
          required: '请填写涉事人员姓名'
        };
        $scope.involvedIdCardNoErrorTips = {
          required: '请填写地址'
        };
        $scope.collectionCasuseErrorTips = {
          required: '请填写采集原因'
        };
        $scope.involedlivingaddressNoErrorTips = {
          required: '请选涉事人员现住地址'
        };

        $scope.eventDescNoErrorTips = {
          required: '请填写事件描述信息'
        };

        $scope.involvedIdCardNoErrorTips = {
          required: '请填写身份证',
          maxlength: '二代身份证为18位',
          minlength: '二代身份证为18位'
        };
        $scope.addressErrorTips = {
          required: '请填写地址'
        };
        $scope.collectionCasuseErrorTips = {
          required: '请填写录入原因'
        };
        $scope.sexErrorTips = {
          required: '请选择性别'
        };
        $scope.eventSourceErrorTips = {
          required: '请选择事件来源'
        };

        $scope.data = {
          involedGener: "1"
        };
        $scope.items = [];
      });
      $scope.$on('$ionicView.afterEnter', function () {
        //TODO采用百度地图进行地位置的获取
        CommonService.getLocation().then(function (gpsLocation) {
          $scope.data.collectionAddressGps = gpsLocation;
          $scope.data.collectionAddress = gpsLocation;
        }, function (error) {

        })

        /**
         * form表单提交
         */
        $scope.submit = function () {

          for (var idx = 0; idx < $scope.items.length; idx++) {
            var imageItem = $scope.items[idx];
            var imageIdx = 'image' + (idx + 1);
            $scope.data[imageIdx] = {};
            $scope.data[imageIdx].data = imageItem;
          }
          AuxiliaryPoliceService.recordEventData($scope.data).then(function (data) {
            $scope.data = {};
            $scope.items = [];
            $cordovaToast.showShortCenter('提交成功');
          }, function (err) {
            $cordovaToast.showShortCenter(err.message);
          })
        }

      });


    }])
  .controller('auxiliaryPoliceRecordListCtrl', ['$scope', function ($scope) {

  }])

  .controller('AuxiliaryPoliceRecordQuery', ['$scope', '$ionicModal', 'CommonService', 'AuxiliaryPoliceService',
    '$cordovaToast', 'AppCache', '$state', '$timeout',
    function ($scope, $ionicModal, CommonService, AuxiliaryPoliceService, $cordovaToast, AppCache, $state, $timeout) {

      $scope.$on('$ionicView.afterEnter', function () {
        $timeout(function () {
          $scope.modal.show();
        }, 222);

      });
      $scope.$on('$ionicView.beforeEnter', function () {
        $ionicModal.fromTemplateUrl('templates/modal/auxiliaryPlistRecordDataListSearchModal.html', {
          scope: $scope,
          animation: 'slide-in-up'
        }).then(function (modal) {
          $scope.modal = modal;
        });


        $scope.personRecords = [];
        $scope.vehicleRecords = [];
        $scope.goodsRecords = [];
        $scope.eventRecords = [];

        $scope.searchCondition = {'collectionType': '人员'};

        /**
         * 调整到详情页面
         * @param id data item id
         */
        $scope.toDetail = function (id) {

          var dataRecords = [];
          var targetState = '';
          switch ($scope.searchCondition.collectionType) {
            case '车辆':
              dataRecords = $scope.vehicleRecords;
              targetState = 'auxiliaryPoliceRecordVehicleDetail';
              break;
            case '物品':
              dataRecords = $scope.goodsRecords;
              targetState = 'auxiliaryPoliceRecordGoodsDetail';
              break;
            case '事件':
              dataRecords = $scope.eventRecords;
              targetState = 'auxiliaryPoliceRecordEventDetail';
              break;
            case '人员':
              dataRecords = $scope.personRecords;
              targetState = 'auxiliaryPoliceRecordPersonDetail';
              break;
          }
          for (var idx = 0; idx < dataRecords.length; idx++) {
            var item = dataRecords[idx];
            if (item.id == id) {
              AppCache.put('recordDetail', item);
              break;
            }
          }
          //界面跳转
          $state.go(targetState);

        };

        $scope.resetBeginTime = function () {
          $scope.searchCondition.beginCollectionTime = '';
        }

        $scope.resetEndTime = function () {
          $scope.searchCondition.endCollectionTime = '';
        }

        $scope.resetAddress = function () {
          $scope.searchCondition.involedlivingaddress = '';
        }
        $scope.resetIDCardNo = function () {
          $scope.searchCondition.involvedIdCardNo = '';
        }

        $scope.submit = function () {
          var collectionType = $scope.searchCondition.collectionType;
          AuxiliaryPoliceService.getRecords($scope.searchCondition).then(function (result) {
            //   $scope.searchCondition = {collectionType:collectionType};
            $scope.modal.hide();

            $scope.personRecords = [];
            $scope.vehicleRecords = [];
            $scope.goodsRecords = [];
            $scope.eventRecords = [];

            switch (collectionType) {
              case '车辆':
                $scope.vehicleRecords = result.records;
                break;
              case '物品':
                $scope.goodsRecords = result.records;
                break;
              case '事件':
                $scope.eventRecords = result.records;
                break;
              case '人员':
                $scope.personRecords = result.records;
                break;
            }

          }, function (error) {
            $cordovaToast.showShortCenter(error.message);
          });
        }

        //选择开始时间
        $scope.inputQueryStartTime = function () {

          CommonService.getTimePicker().then(function (timeString, date) {
            $scope.searchCondition.beginCollectionTime = timeString;

          });
        };
        $scope.inputQueryEndTime = function () {
          CommonService.getTimePicker().then(function (timeString, date) {
            $scope.searchCondition.endCollectionTime = timeString;
          });
        };

      });


    }])

  .controller('AuxiliaryPoliceRecordQueryCtrl', ['$scope', '$cacheFactory', function ($scope, $cacheFactory) {

  }])
  .controller('AuxiliaryPoliceRecordEventDetailCtrl', ['$scope', 'AppCache','AppSettings', function ($scope, AppCache,AppSettings) {
    $scope.$on('$ionicView.afterEnter', function () {

    });
    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.detailRecord = AppCache.get('recordDetail');
      $scope.items = [];
      for (var idx = 1; idx <= 10; idx++) {
        var imageIdx = 'image' + idx;
        var image = $scope.detailRecord[imageIdx];
        if (image && image.id) {
          var imageUrl = AppSettings.resEndpoint + "?imageId=" + image.id;
          $scope.items.push(imageUrl);
        }
      }
    });
    $scope.$on('$ionicView.afterLeave', function () {
      AppCache.remove('recordDetail');
    });
  }])
  .controller('AuxiliaryPoliceRecordGoodsDetailCtrl', ['$scope', 'AppCache','AppSettings', function ($scope, AppCache,AppSettings) {
    $scope.$on('$ionicView.afterEnter', function () {

    });
    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.detailRecord = AppCache.get('recordDetail');
      $scope.items = [];
      for (var idx = 1; idx <= 10; idx++) {
        var imageIdx = 'image' + idx;
        var image = $scope.detailRecord[imageIdx];
        if (image && image.id) {
          var imageUrl = AppSettings.resEndpoint + "?imageId=" + image.id;
          $scope.items.push(imageUrl);
        }
      }
    });
    $scope.$on('$ionicView.afterLeave', function () {
      AppCache.remove('recordDetail');
    });
  }])
  .controller('AuxiliaryPoliceRecordVehicleDetailCtrl', ['$scope', 'AppCache', 'AppSettings', function ($scope, AppCache, AppSettings) {

    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.detailRecord = AppCache.get('recordDetail');
      $scope.items = [];
      for (var idx = 1; idx <= 10; idx++) {
        var imageIdx = 'image' + idx;
        var image = $scope.detailRecord[imageIdx];
        if (image && image.id) {
          var imageUrl = AppSettings.resEndpoint + "?imageId=" + image.id;
          $scope.items.push(imageUrl);
        }
      }
    });
    $scope.$on('$ionicView.afterLeave', function () {

      AppCache.remove('recordDetail');
    });

  }])
  .controller('AuxiliaryPoliceRecordPersonDetailCtrl', ['$scope', 'AppCache', 'AppSettings', function ($scope, AppCache, AppSettings) {

    $scope.$on('$ionicView.beforeEnter', function () {
      $scope.detailRecord = AppCache.get('recordDetail');
      $scope.items = [];
      for (var idx = 1; idx <= 10; idx++) {
        var imageIdx = 'image' + idx;
        var image = $scope.detailRecord[imageIdx];
        if (image && image.id) {
          var imageUrl = AppSettings.resEndpoint + "?imageId=" + image.id;
          $scope.items.push(imageUrl);
        }
      }
    });
    $scope.$on('$ionicView.afterLeave', function () {

      AppCache.remove('recordDetail');
    });

  }])
;
